﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.Filters;
using GestionePolveriLib.Facades;
using GestionePolveriLib.BL;
using GestionePolveriLib.Utilities;
using GestionePolveriLib.Facades.Extensions;

namespace GestionePolveri2.Filters
{
    public partial class CronologiaFilter2 : CronologiaFilterBase
    {
        public CronologiaFilter2()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                RefreshData();

                //    m_periodFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
                //    m_periodTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
                this.FromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
                this.ToDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
                this.UseDatesFilter = true;
            }
        }

        protected override void OnResetFilter(EventArgs e)
        {
            this.cmbFornitori.SelectedIndex = 0;
            this.cmbLotti.SelectedIndex = 0;
            this.cmbPolveri.SelectedIndex = 0;
            this.cmbCausaliScarico.SelectedIndex = 0;

            FilterBag.CodiceLotto = null;
            FilterBag.CodicePolvere = null;
            FilterBag.Operatore = null;
            FilterBag.Fornitore = null;

            // particolare comportamento della cronologia che deve mostrare di default i movimenti del mese corrente...
            this.FromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
            this.ToDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
            this.UseDatesFilter = true;

            base.OnResetFilter(e);
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            if (this.cmbLotti.SelectedItem == null)
                this.cmbLotti.SelectedIndex = 0;
            if (this.cmbPolveri.SelectedItem == null)
                this.cmbPolveri.SelectedIndex = 0;
            if (this.cmbFornitori.SelectedItem == null)
                this.cmbFornitori.SelectedIndex = 0;
            if (this.cmbCausaliScarico.SelectedItem == null)
                this.cmbCausaliScarico.SelectedIndex = 0;

            FilterBag.CausaleScarico = (((CausaleScarico)this.cmbCausaliScarico.SelectedItem).ID == -1 ? null : ((CausaleScarico)this.cmbCausaliScarico.SelectedItem).CodCausale);
            FilterBag.CodiceLotto = (((Lotto)this.cmbLotti.SelectedItem).IDLotto == -1 ? null : ((Lotto)this.cmbLotti.SelectedItem).CodLotto);
            FilterBag.CodicePolvere = (((Polvere)this.cmbPolveri.SelectedItem).ID == -1 ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.Operatore = (string.IsNullOrWhiteSpace(this.txtOperatore.Text) ? null : this.txtOperatore.Text);
            FilterBag.Fornitore = (((Fornitore)this.cmbFornitori.SelectedItem).ID == -1 ? null : ((Fornitore)this.cmbFornitori.SelectedItem).Descrizione);
            FilterBag.PeriodFrom = this.FromDate;
            FilterBag.PeriodTo = this.ToDate;
            FilterBag.UseDates = this.UseDatesFilter;

            if (
                (this.cmbLotti.SelectedIndex == 0) &&
                (this.cmbPolveri.SelectedIndex == 0) &&
                (this.cmbFornitori.SelectedIndex == 0) &&
                (this.cmbCausaliScarico.SelectedIndex == 0) &&
                (FilterBag.CodiceLotto == null) &&
                (FilterBag.CodicePolvere == null) &&
                (FilterBag.Operatore == null) &&
                (FilterBag.Fornitore == null) &&
                (FilterBag.CausaleScarico == null) &&
                (FilterBag.UseDates == false)
              )
            {
                //OnResetFilter(e);
                base.OnExecuteFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);
        }

        private void LoadFornitori()
        {
            FilterComboBoxHelper.SetupFornitoriComboBox(this.cmbFornitori);
        }

        public void LoadLotti()
        {
            FilterComboBoxHelper.SetupLottiComboBox(this.cmbLotti);
        }

        public void LoadCausaliScarico()
        {
            FilterComboBoxHelper.SetupCausaliScaricoComboBox(this.cmbCausaliScarico);
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadFornitori();
            LoadLotti();
            LoadCausaliScarico();
        }
    }

    public class CronologiaFilterBase : FilterContainerBase2<CronologiaFilterBag> { }
}
